#include "udpServer.hpp"
#include <memory>
#include <unordered_map>
#include<fstream>
#include<signal.h>
using namespace std;
using namespace Server;

static void usage(std::string proc)
{
    std::cout << "\nUsage:\n\t" << proc << " local_port\n\t\n";
}



void handlerMessage(int sockfd,string clientip,uint16_t clientport,string messages)
{
    string respons_message = messages;
    respons_message +=" [server echo]";
    //我们接下来就可以将消息发回去！
    struct sockaddr_in peer;
    bzero(&peer,sizeof(peer));//清0
    peer.sin_family = AF_INET;
    peer.sin_port = htons(clientport);
    peer.sin_addr.s_addr = inet_addr(clientip.c_str());

    sendto(sockfd,respons_message.c_str(),respons_message.size(),0,(struct sockaddr*)&peer,sizeof(peer));
}




int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        usage(argv[0]);
        exit(USAGE_ERR);
    }
    uint16_t port = atoi(argv[1]);//将字符串转换为整数
    
    std::unique_ptr<udpServer> server(new udpServer(handlerMessage,port));

    server->initServer();//初始化服务端
    server->start();//启动服务端

    return 0;
}